package com.nidu.demo.log.config;

import com.nidu.demo.log.aop.ApiAccessLogAspect;
import com.nidu.demo.log.aop.OperateLogAspect;
import com.nidu.demo.log.gateway.ApiAccessLogGateway;
import com.nidu.demo.log.gateway.OperateLogGateway;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * 日志AOP自动配置类
 *
 * @author auto
 */
@Slf4j
@Configuration
@EnableAspectJAutoProxy
public class LogAopAutoConfiguration {

    /**
     * API访问日志切面
     */
    @Bean
    @ConditionalOnProperty(name = "cola.log.api-access.enabled", havingValue = "true", matchIfMissing = true)
    public ApiAccessLogAspect apiAccessLogAspect(ApiAccessLogGateway apiAccessLogGateway) {
        log.info("初始化API访问日志切面");
        return new ApiAccessLogAspect(apiAccessLogGateway);
    }

    /**
     * 操作日志切面
     */
    @Bean
    @ConditionalOnProperty(name = "cola.log.operate.enabled", havingValue = "true", matchIfMissing = true)
    public OperateLogAspect operateLogAspect(OperateLogGateway operateLogGateway) {
        log.info("初始化操作日志切面");
        return new OperateLogAspect(operateLogGateway);
    }
}
